package com.csy.user.controller.system;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.csy.commons.core.controller.BaseController;
import com.csy.commons.core.domain.BaseResponse;
import com.csy.commons.core.page.PageResult;
import com.csy.user.entity.system.domain.AjVisitor;
import com.csy.user.entity.system.service.AjVisitorService;

import cn.hutool.core.util.IdUtil;

/**
 * 访客控制类
 */
@RestController
@RequestMapping("/system/visitor")
public class AjVisitorController extends BaseController {

    @Resource
    private AjVisitorService ajVisitorService;

    /**
     * 查询访客业主
     * @param ajVisitor
     * @return
     */
    @GetMapping("/list")
    public PageResult getList(AjVisitor ajVisitor){
        startPage();
        List<AjVisitor> list = ajVisitorService.list(ajVisitor);
        return getData(list);

    }

    /**
     * 根据id查访客
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public BaseResponse<AjVisitor> getList(@PathVariable("id") Long id){
        AjVisitor ajVisitor = ajVisitorService.getById(id);
        return BaseResponse.success(ajVisitor);

    }

    /**
     * 新增访客
     * @param ajVisitor
     * @return
     */
    @PostMapping
    public BaseResponse<Integer> add(@RequestBody AjVisitor ajVisitor){
        ajVisitor.setVisitorId(IdUtil.getSnowflakeNextId());
        ajVisitor.setCreateTime(new Date());
        boolean b = ajVisitorService.save(ajVisitor);
        return toAjax(b == true ? 1 : 0);
    }

    /**
     * 更新访客信息
     * @param ajVisitor
     * @return
     */
    @PutMapping
    public BaseResponse<Integer> update(@RequestBody AjVisitor ajVisitor){
        boolean b = ajVisitorService.updateById(ajVisitor);
        return toAjax(b == true ? 1 : 0);
    }

    /**
     * 删除访客信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public BaseResponse<Integer> del(@PathVariable("id")String id){
        boolean b = ajVisitorService.removeById(id);
        return toAjax(b == true ? 1 : 0);
    }

}
